/**
 * 初始化公司资料详情对话框
 */
var CompanyInfoDlg = {
    companyInfoData : {},
    validateFields: {
    	companyName: {
            validators: {
                notEmpty: {
                    message: '公司名称不能为空'
                }
            }
        },
        companyTell: {
            validators: {
                notEmpty: {
                    message: '公司电话不能为空'
                }
            }
        },
        companyDepositName: {
            validators: {
                notEmpty: {
                    message: '开户行名称不能为空'
                }
            }
        },
        companyDepositId: {
            validators: {
                notEmpty: {
                    message: '开户行账号不能为空'
                }
            }
        },
        companyContactTell: {
            validators: {
                notEmpty: {
                    message: '联系人电话不能为空'
                }
            }
        }
    }
};
/**
 * 验证数据是否为空
 */
CompanyInfoDlg.validate = function () {
    $('#companyForm').data("bootstrapValidator").resetForm();
    $('#companyForm').bootstrapValidator('validate');
    return $("#companyForm").data('bootstrapValidator').isValid();
};
/**
 * 清除数据
 */
CompanyInfoDlg.clearData = function() {
    this.companyInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CompanyInfoDlg.set = function(key, val) {
    this.companyInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CompanyInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
CompanyInfoDlg.close = function() {
    parent.layer.close(window.parent.Company.layerIndex);
}

/**
 * 收集数据
 */
CompanyInfoDlg.collectData = function() {
    this
    .set('id')
    .set('companyId')
    .set('companyLegalPerson')
    .set('companyName')
    .set('companyTell')
    .set('companyFax')
    .set('companyCountry')
    .set('companyProvince')
    .set('companyCity')
    .set('companyArea')
    .set('companyPlace')
    .set('companyWebsite')
    .set('companyCreditCode')
    .set('companyDepositName')
    .set('companyDepositId')
    .set('companyContactName')
    .set('companyContactTell')
    .set('companyAmount')
    .set('companyMoney');
}

/**
 * 提交添加
 */
CompanyInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/company/add", function(data){
        Feng.success("添加成功!");
        window.parent.Company.table.refresh();
        CompanyInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.companyInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
CompanyInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/company/update", function(data){
        Feng.success("修改成功!");
        window.parent.Company.table.refresh();
        CompanyInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.companyInfoData);
    ajax.start();
}

$(function() {
	Feng.initValidator("companyForm", CompanyInfoDlg.validateFields);
});
